package com.proximity.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.proximity.R;
import com.proximity.business.ItemOverlay;
import com.proximity.utils.LocationConversion;

public class Application extends MapActivity implements LocationListener{

	private static GeoPoint myPoint = null;
	
	private Timer timer = new Timer();
	private static final int UPDATE_INTERVAL = 10000;
	private static MapView mapView = null;
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    //listener for location
	    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
	    
	    //update map every 10seg
	    timer.scheduleAtFixedRate(
	  	      new TimerTask() {
	  	        public void run() {
	  	          refreshMap();
	  	        }
	  	      },
	  	      0,
	  	      UPDATE_INTERVAL);
	}
	
	@Override
	public void onLocationChanged(Location loc) {
		if (loc!=null) {
			myPoint = new GeoPoint(LocationConversion.toMicroDegrees(loc.getLatitude()),LocationConversion.toMicroDegrees(loc.getLongitude()));
		}
	}
	
	private void refreshMap() {
		if (mapView!=null) {
			//mapView.getController().animateTo(myPoint);
			
			List<GeoPoint> otherGeoPoints = null;
			if(mapView.getLatitudeSpan()!=0 && mapView.getLongitudeSpan()!=360000000) {
				//llamar a json para guardar la posicion y obtener los cercanos dependiendo del latitude span y longitudespan
				otherGeoPoints = new ArrayList<GeoPoint>();
				otherGeoPoints.add(new GeoPoint(LocationConversion.toMicroDegrees(41.387917),LocationConversion.toMicroDegrees(2.1699187)));
				otherGeoPoints.add(new GeoPoint(LocationConversion.toMicroDegrees(41.4479366),LocationConversion.toMicroDegrees(2.179537)));
				otherGeoPoints.add(new GeoPoint(LocationConversion.toMicroDegrees(37.38264),LocationConversion.toMicroDegrees(-5.9962951)));
			}
			
			setOverlayPosition(myPoint, otherGeoPoints);
			
//			mapView.getController().setZoom(15);
			
			mapView.postInvalidate();
		}
		
	}
	
	private void setOverlayPosition(GeoPoint myGeoPoint, List<GeoPoint> otherGeoPoint) {
		
		ItemOverlay myOverlay = null;
		if (myGeoPoint!=null) {
			Drawable myDrawable = this.getResources().getDrawable(R.drawable.androidblue);
			myOverlay = new ItemOverlay(myDrawable, this);
			myOverlay.addOverlay(new OverlayItem(myGeoPoint,"",""));
		}
		
		ItemOverlay otherOverlay = null;
		if (otherGeoPoint!=null) {
			Drawable otherDrawable = this.getResources().getDrawable(R.drawable.androidmarker);
			otherOverlay = new ItemOverlay(otherDrawable, this);
			for (GeoPoint geoPoint : otherGeoPoint) {
				otherOverlay.addOverlay(new OverlayItem(geoPoint,"",""));
		    }
		}
	    
		mapView.getOverlays().clear();
		if (myOverlay!=null)
			mapView.getOverlays().add(myOverlay);
		if (otherOverlay!=null)
			mapView.getOverlays().add(otherOverlay);
		
	}
	
	@Override
	public void onProviderDisabled(String arg0) {
	}
	
	@Override
	public void onProviderEnabled(String arg0) {
	}
	
	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}
}
